<?php

namespace app\admin\controller\union;

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\admin\model\union\GuildWithdrawAccount;
use app\common\controller\Backend;
use think\Db;

/**
 * 提现账户管理
 *
 */
class Inaccount extends Backend
{

    /**
     * @var \app\admin\model\Admin
     */
    protected $model = null;
    protected $admin_id = null;
    protected $guild_id = null;
//    protected $selectpageFields = 'id,username,nickname,avatar';
//    protected $searchFields = 'id,username,nickname';

    public function _initialize()
    {
        parent::_initialize();
        //获取后台登录管理员id
        $auth = Auth::instance();
        $this->admin_id = $auth->isLogin() ? $auth->id : 0;
        $this->guild_id = Admin::get(intval($auth->id))['guild_id'];
        $this->assign('admin_id', $this->admin_id);
        $this->model = new GuildWithdrawAccount();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model->where($where);
            if($this->guild_id){
                $list = $list->where('guild_manage_id',$this->guild_id);
            }
            $list = $list->order($sort, $order)->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                Db::startTrans();
                try {
                    $params['guild_manage_id'] = $this->guild_id;
                    $params['created_at'] = date('Y-m-d H:i:s');
                    $params['updated_at'] = date('Y-m-d H:i:s');
                    $result = $this->model->save($params);
                    if ($result === false) {
                        exception($this->model->getError());
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    if(isset($params['status']) && $params['status'] == 1){ //启用账号，其他账号强制停用
                        $this->model->where(['status'=>1])->update(['status'=>2]);
                    }
                    //姓名或者账号有改动，需要审核
                    if($row->username != $params['username'] || $row->payment != $params['payment']){
                        $params['status'] = 0;
                    }
                    $params['updated_at'] = date('Y-m-d H:i:s');
                    $result = $row->save($params);
                    if ($result === false) {
                        exception($row->getError());
                    }
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
                $deleteIds = explode(',', $ids);
                try {
                    $this->model->destroy($deleteIds);
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
        }
        $this->error(__('No rows were deleted'));
    }

    /**
     * 批量更新
     * @internal
     */
    public function multi($ids = "")
    {
        // 管理员禁止批量操作
        $this->error();
    }

    /**
     * 下拉搜索
     */
    public function selectpage()
    {
        $this->dataLimit = 'auth';
        $this->dataLimitField = 'id';
        return parent::selectpage();
    }
}
